package com.buba.system.controller;

import com.alibaba.fastjson.JSON;
import com.buba.system.pojo.Job;
import com.buba.system.pojo.Menu;
import com.buba.system.pojo.Page;
import com.buba.system.service.JobService;
import com.buba.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职位控制层
 */
@RestController
@RequestMapping("/JobController")
public class JobController {
    private JobService jobService;
    private MenuService menuService;

    @RequestMapping("/selAllJob")
    public Map<String, Object> selAllJob(){
        Map<String, Object> map = new HashMap<>();
        List<Job> list = jobService.selAllJob();
        map.put("list",list);
        return map;
    }



    @RequestMapping("/addJob")
    public Map<String,Object> addJob(String jobTitle,String nodeArray){
        //js中json对象转json字符串方法JSON.stringify(nodeArray)
        //java中json字符串转java对象：JSON.parseArray()
        //把json字符串转为数组
        List<Menu> list = JSON.parseArray(nodeArray,Menu.class);
        list.forEach(System.out::println);
        //添加职位名称  mybatis添加时返回主键 参数必须是对象或者Map
        //返回添加的主键不是返回值的形式,而是把主键直接赋值到传入的参数中
        Job job = new Job();
        job.setJobTitle(jobTitle);
        Integer count = jobService.addJob(job);
        System.out.println("添加的职位ID" + job);
        //给职位添加菜单权限
        Map<String,Object> map = new HashMap<>();
        map.put("jobId",job.getJobId());//获取职位（job表）Id
        map.put("menuList",list);//把list得到的菜单信息（menuId）存入map
        //获取到jobId跟menuId把信息存到中间表（job_menu）
        Integer menuCount = menuService.addMenuByJobId(map);
        if (count != null){
            map.put("message","添加了"+count+"职位");
        }else{
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     * 删除员工职位及菜单
     * @param ids
     * @return
     */
    @RequestMapping("/delJobTitle")
    public Map<String,Object> delJobTitle(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);//Arrays.asList()将数组转化为list
        //先删emp_job表id
        Integer delEmpJobId = jobService.delEmpJobId(list);
        //job_menu表id
        Integer delEmpJobEmId = menuService.delEmpJobEmId(list);
        //最后删员工表
        Integer count = jobService.delJobTitle(list);
        if (count != null){
            map.put("message","删除"+count+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     * 修改职位的菜单权限
     * @param jobId
     * @param nodeArray
     * @return
     */
    @RequestMapping("/updJobMenu")
    public Map<String,Object> updJobMenu(Integer jobId,String nodeArray){
        //1、把该职位现有的菜单权限清空
        Integer count = menuService.delMenuByJobId(jobId);
        //2、添加新的权限
        Map<String,Object> map = new HashMap<>();
        map.put("jobId",jobId);
        List<Menu> list = JSON.parseArray(nodeArray,Menu.class);
        map.put("menuList",list);
        menuService.addMenuByJobId(map);
        return map;
    }

    @Autowired
    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }

    @Autowired
    public void setJobService(JobService jobService) {
        this.jobService = jobService;
    }
}
